package sgi.client;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Image;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import sgi.details_new.ImageDetails;
import sgi.message.MessagePurchaseImage;

import java.awt.Canvas;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import javax.swing.JScrollPane;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Font;

public class SGIImageViewer extends JFrame {

	private JPanel contentPane;
	private ImageIcon ImageIcon;
	private ImageIcon CommerceIcon;
	private ImageIcon InfrastractureIcon;
	private ImageIcon CommerceInfrastractureIcon;
	public static SGIImageViewer viewer;
	private JLabel imageLabel;
	private JScrollPane scrollPane;
	private float scale=1;
	private ImageDetails details;
	private JButton btnCommerce;
	private boolean btnCommercePressd;
	private JButton btnInfrastracture;
	private boolean btnInfrastracturePressd;
	private ImageIcon BtnNormal;
	private ImageIcon BtnPressed;
	
	
	
	public SGIImageViewer(ImageDetails details) {
		
			setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			setBounds(100, 100, 748, 577);
			contentPane = new JPanel();
			contentPane.setLayout(null);
			setContentPane(contentPane);
			this.details = new ImageDetails(details);
			
			
			
	    	ImageIcon = new ImageIcon(System.getProperty("user.home")+"\\My Documents\\SGIimages." +SGIUserAccountMenuController.getUser().getUsername() + "\\" +  details.getType() + "%" + String.valueOf(details.getImageid()) + "%" + details.getDate() + "%" + details.getLocation() + "%" +".jpeg");
	    	
	    	if(details.getType().equals("Image"))
	    	{
	    		File fcommerce = new File(System.getProperty("user.home")+"\\My Documents\\SGIimages." +SGIUserAccountMenuController.getUser().getUsername()+ "\\Commerce%" + String.valueOf(details.getImageid()) + "%" + details.getDate() + "%" + details.getLocation() + "%" +".jpeg");
	    		if(fcommerce.exists())
	    			setCommerceIcon(new ImageIcon(fcommerce.getPath()));
	    		else
	    			setCommerceIcon(null);
	    		
	    		File finfrastracture = new File(System.getProperty("user.home")+"\\My Documents\\SGIimages." +SGIUserAccountMenuController.getUser().getUsername()+ "\\Infrastracture%" + String.valueOf(details.getImageid()) + "%" + details.getDate() + "%" + details.getLocation() + "%" +".jpeg");
	    		if(finfrastracture.exists())
	    			setInfrastractureIcon(new ImageIcon(finfrastracture.getPath()));
	    		else
	    			setInfrastractureIcon(null);
	    	
	    		File fcommerceinfrastracture = new File(System.getProperty("user.home")+"\\My Documents\\SGIimages." +SGIUserAccountMenuController.getUser().getUsername()+ "\\CommerceInfrastracture%" + String.valueOf(details.getImageid()) + "%" + details.getDate() + "%" + details.getLocation() + "%" +".jpeg");
	    		if(fcommerceinfrastracture.exists())
	    			setCommerceInfrastractureIcon(new ImageIcon(fcommerceinfrastracture.getPath()));
	    		else
	    			setCommerceInfrastractureIcon(null);
	    	}
	    	
			scrollPane = new JScrollPane();
			scrollPane.setBounds(10, 11, 712, 455);
			contentPane.add(scrollPane);
			
			imageLabel = new JLabel(ImageIcon);
			scrollPane.setViewportView(imageLabel);
			
			JButton btnZoomOut = new JButton("-");
			btnZoomOut.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					zoomOut();
				}
			});
			btnZoomOut.setFont(new Font("Tahoma", Font.BOLD, 22));
			btnZoomOut.setBounds(293, 477, 51, 40);
			contentPane.add(btnZoomOut);
			
			JButton buttonZoomIn = new JButton("+");
			buttonZoomIn.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					zoomIn();
				}
			});
			buttonZoomIn.setFont(new Font("Tahoma", Font.BOLD, 20));
			buttonZoomIn.setBounds(354, 477, 51, 40);
			contentPane.add(buttonZoomIn);
			
			JButton btnClose = new JButton("Close");
			btnClose.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					SGIImageViewer.viewer.close();
				}
			});
			btnClose.setBounds(632, 477, 90, 37);
			contentPane.add(btnClose);
			
			btnCommerce = new JButton("Commerce");
			btnCommerce.setIcon(null);
			if(getCommerceIcon() == null || (details.getType().equals("Sample")))
					btnCommerce.setEnabled(false);
			btnCommerce.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					
					changeInfrastractureButtonState();
					setLayers();
					
				}
			});
			btnCommerce.setBounds(10, 477, 113, 37);
			contentPane.add(btnCommerce);
			
			btnInfrastracture = new JButton("Infrastracture");
			if(getInfrastractureIcon() == null || (details.getType().equals("Sample")))
				btnInfrastracture.setEnabled(false);
			btnInfrastracture.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					
					changeInfrastractureButtonState();
					setLayers();
					
				}
			});
			if(getInfrastractureIcon() == null || (details.getType().equals("Sample")))
				btnCommerce.setEnabled(false);
			btnInfrastracture.setBounds(135, 477, 113, 37);
			contentPane.add(btnInfrastracture);
	
	}
	
	public void zoomIn()
	{
		if(scale < 3)
		{			
			scale+=0.2;
			Image img = ImageIcon.getImage();
			Image newimg = img.getScaledInstance((int)(img.getWidth(null)*scale), (int)(img.getHeight(null)*scale),  java.awt.Image.SCALE_SMOOTH);
			ImageIcon NewImageIcon =  new ImageIcon(newimg); 
			imageLabel = new JLabel(NewImageIcon);
			scrollPane.setViewportView(imageLabel);
		}
	}
	
	public void zoomOut()
	{
		if(scale > 0.2)
		{
			scale-=0.2;
			Image img = ImageIcon.getImage();
			Image newimg = img.getScaledInstance((int)(img.getWidth(null)*scale), (int)(img.getHeight(null)*scale),  java.awt.Image.SCALE_SMOOTH);
			ImageIcon NewImageIcon =  new ImageIcon(newimg); 
			imageLabel = new JLabel(NewImageIcon);
			scrollPane.setViewportView(imageLabel);
		}
	}
	
	public void setLayers()
	{
		if((btnCommercePressd == false) && (btnInfrastracturePressd == false))
		{
			imageLabel = new JLabel(ImageIcon);
			scrollPane.setViewportView(imageLabel);
		}
		else if((btnCommercePressd == true) && (btnInfrastracturePressd == false))
		{
			imageLabel = new JLabel(CommerceIcon);
			scrollPane.setViewportView(imageLabel);
		}
		else if((btnCommercePressd == false) && (btnInfrastracturePressd == true))
		{
			imageLabel = new JLabel(InfrastractureIcon);
			scrollPane.setViewportView(imageLabel);
		}
		else 
		{
			imageLabel = new JLabel(CommerceInfrastractureIcon);
			scrollPane.setViewportView(imageLabel);
		}
	}
	

	public void close()
	{
		SGIImageViewer.viewer.setVisible(false);
		SGIImageViewer.viewer = null;
		
	}
	

	public ImageIcon getInfrastractureIcon() {
		return InfrastractureIcon;
	}

	public void setInfrastractureIcon(ImageIcon infrastractureIcon) {
		InfrastractureIcon = infrastractureIcon;
	}

	public ImageIcon getCommerceIcon() {
		return CommerceIcon;
	}

	public void setCommerceIcon(ImageIcon commerceIcon) {
		CommerceIcon = commerceIcon;
	}

	public ImageIcon getCommerceInfrastractureIcon() {
		return CommerceInfrastractureIcon;
	}

	public void setCommerceInfrastractureIcon(ImageIcon infrastractureCommerceIcon) {
		CommerceInfrastractureIcon = infrastractureCommerceIcon;
	}

	
	public void changeCommerceButtonState()
	{
		if(btnCommercePressd == true)
		{
			btnCommercePressd = false;
			btnCommerce.setPressedIcon(null);
		}
		else
		{
			btnCommercePressd = true;
			btnCommerce.setIcon(null);
		}
	}
	
	public void changeInfrastractureButtonState()
	{
		if(btnInfrastracturePressd == true)
		{
			btnInfrastracturePressd = false;
			btnInfrastracture.setPressedIcon(null);
		}
		else
		{
			btnInfrastracturePressd = true;
			btnInfrastracture.setIcon(null);
		}
	}
	


}
